// pages/tixian/tixian.js
const app = getApp();
// 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['微信', '支付宝(推荐)'],
    index: '',
    fangshi: [
      {
        'id': '1',
        'img': 'https://weiqing.yiyawlkj.com/xhyg/img/wxhui.png',
        'seleced_img': 'https://weiqing.yiyawlkj.com/xhyg/img/wxhong.png'
      },
      {
        'id': '2',
        'img': 'https://weiqing.yiyawlkj.com/xhyg/img/yhkhong.png',
        'seleced_img': 'https://weiqing.yiyawlkj.com/xhyg/img/yhkhui.png'
      }
    ],
    fangshi1: [],
    hidden_value: true,
    button: true,
    DrawType:"微信"
  },
  // 支付宝、微信
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value); //index为数组点击确定后选择的item索引
    this.setData({
      index: e.detail.value,
      DrawType:this.data.array[e.detail.value]
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(wx.getStorageSync(userInfo))
    var that = this;
    var L = [];
    L = that.data.fangshi;
    that.setData({
      fangshi1: L,
      shouruhe: options.shouruhe,
      type: 1,
    });
    var userId = wx.getStorageSync('userId');
    that.system();//获取系统参数
    that.getUser(userId);//读取会员
    console.log(that.data.shouruhe,'余额')
  },
  getUser: function (userId) {//读取会员
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'user/getUser',
      data: {
        // opt: 'getUser',
        userId: userId,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data != null) {
          that.setData({
            islock: res.data.IsLock,
            user:res.data,
          })
          
        }
      }

    })
    console.log(that.data.canIUse)
  },
  //获取系统参数
  system:function(){
    var that=this;
    wx.request({
      url: app.globalData.apiUrl +'User/getsystem',
      success:function(res){
        
        that.setData({
          system:res.data,
        })
        console.log(that.data.system['Id'], '系统参数')
      }
    })
  },
  //提现提交
  formSubmit:function(e){
    var that=this;
    var formData = e.detail.value;
    formData['DrawType']=that.data.DrawType;
    formData['MemberCode'] = that.data.user.Code;
    formData['NickName'] = that.data.user.NickName;
    formData['OpenBank']=that.data.user.OpenBank;
    //手续费
    // console.log(zhi,'提现金额')
    // console.log(shouruhe, '余额')

    if(that.data.islock==1){
      wx.showToast({
        title: '提现已锁定',
        icon: 'none',
      })
      return;
    }
   
    if(wx.getStorageSync('userId')!==""){
      if (formData['bankcode'] == "" || formData['BankAccount'] == "" || formData['DrawAmount'] == ""){
        wx.showToast({
          title: '信息必填',
          icon:'none',
        })
        return;
      } 
       //else if (zhi==""){
      //   wx.showToast({
      //     title: '金额不可为空',
      //     icon: 'none',
      //   })
      //   return;
      // } else if (zhi < that.data.system['MinAmount']){
      //   wx.showToast({
      //     title: '余额必须大于' + that.data.system['MinAmount'],
      //     icon: 'none',
      //   })
      //   return;
        
      // }
      // if (zhi * 1 > shouruhe) {
      //   console.log(zhi, '提现金额')

      //   wx.showToast({
      //     title: '余额不足',
      //     icon: 'loading',
      //     duration: 1500
      //   })
      //   return;
      // } else {
        that.setData({
          hidden:false,
          
        })
        if (that.data.button == true) {
          var userId = wx.getStorageSync('userId');
          formData['MemberId'] = userId;

          //添加提现
          wx.request({
            url: app.globalData.apiUrl + 'User/addtixian',
            data: formData,
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              // console.log(res)
              if (res.data.status == 1) {
                that.setData({
                  hidden_value: false,
                  shouruhe: that.data.shouruhe - formData['DrawAmount']
                });
                wx.showToast({
                  title: '提现申请成功',
                  icon: 'none',
                  duration: 1500,
                  success: function () {
                    wx.navigateTo({
                      url: '../couponlist/couponlist?type=' + that.data.type,
                    })
                  }
                })

              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'icon',
                  duration: 1500,
                  success : function () {
                    that.setData({
                      button: true,
                    })
                  }
                })
              }
            }
          })
        }
        

      // }

    }else{
      wx.showModal({
        title: '提示',
        content: '请先登录',
        success:function(){
          wx.switchTab({
            url: '/pages/user/user',
          })
        }
      })
    }
  },
  
  gh_img: function (e) {
    var that = this;
    var fangshi = that.data.fangshi;
    var r = e.currentTarget.dataset.id;
    var L = [];
    var n = '';
    var m = '';
    for (var i in fangshi) {
      if (fangshi[i]['id'] == r) {
        n = fangshi[i]['img'];
        fangshi[i]['img'] = fangshi[i]['seleced_img'];
        fangshi[i]['seleced_img'] = n;
      }
      if (fangshi[i]['id'] != r) {
        m = fangshi[i]['img'];
        fangshi[i]['img'] = fangshi[i]['seleced_img'];
        fangshi[i]['seleced_img'] = m;
      }
      L.push(fangshi[i]);
    }
    that.setData({
      fangshi1: L,
      tixiantype: r,
    });
  },
  //全部提现
  quanbutixian: function () {
    console.log(11)
    var that = this;
    that.setData({
      amount: that.data.shouruhe,
    });
  },
  getamount: function (e) {
    console.log(e)
    var that = this;
    that.setData({
      amount: e.detail.value,
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      button:true
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
 
})